package com.yixinsoft.basic.web.taglib;

import java.util.Collection;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.yixinsoft.basic.model.manage.system.Organization;
import com.yixinsoft.basic.model.manage.system.Resource;
import com.yixinsoft.basic.model.manage.system.Role;
import com.yixinsoft.basic.service.manage.system.OrganizationService;
import com.yixinsoft.basic.service.manage.system.ResourceService;
import com.yixinsoft.basic.service.manage.system.RoleService;
import com.yixinsoft.basic.spring.SpringUtils;

/**
 * Created by hqy on 2016/06/10
 */
public class Functions {
	
	@SuppressWarnings("rawtypes")
	public static boolean in(Iterable iterable, Object element) {
		if (iterable == null) {
			return false;
		}
		return CollectionUtils.contains(iterable.iterator(), element);
	}
	
	public static String organizationName(Long organizationId) {
		Organization organization = getOrganizationService().queryById(organizationId);
		if (organization == null) {
			return "";
		}
		return organization.getName();
	}
	
	public static String organizationNames(Collection<Long> organizationIds) {
		if (CollectionUtils.isEmpty(organizationIds)) {
			return "";
		}
		
		StringBuilder s = new StringBuilder();
		for (Long organizationId : organizationIds) {
			Organization organization = getOrganizationService().queryById(organizationId);
			if (organization == null) {
				return "";
			}
			s.append(organization.getName());
			s.append(",");
		}
		
		if (s.length() > 0) {
			s.deleteCharAt(s.length() - 1);
		}
		
		return s.toString();
	}
	
	public static String roleName(Long roleId) {
		Role role = getRoleService().queryById(roleId);
		if (role == null) {
			return "";
		}
		return role.getDescription();
	}
	
	public static String roleNames(Collection<Long> roleIds) {
		if (CollectionUtils.isEmpty(roleIds)) {
			return "";
		}
		
		StringBuilder s = new StringBuilder();
		for (Long roleId : roleIds) {
			Role role = getRoleService().queryById(roleId);
			if (role == null) {
				return "";
			}
			s.append(role.getDescription());
			s.append(",");
		}
		
		if (s.length() > 0) {
			s.deleteCharAt(s.length() - 1);
		}
		
		return s.toString();
	}
	
	public static String resourceName(Long resourceId) {
		Resource resource = getResourceService().queryById(resourceId);
		if (resource == null) {
			return "";
		}
		return resource.getName();
	}
	
	public static String resourceNames(Collection<Long> resourceIds) {
		if (CollectionUtils.isEmpty(resourceIds)) {
			return "";
		}
		
		StringBuilder s = new StringBuilder();
		for (Long resourceId : resourceIds) {
			Resource resource = getResourceService().queryById(resourceId);
			if (resource == null) {
				return "";
			}
			s.append(resource.getName());
			s.append(",");
		}
		
		if (s.length() > 0) {
			s.deleteCharAt(s.length() - 1);
		}
		
		return s.toString();
	}
	
	
	/**
	 * 数字转大写字母
	 * @param input
	 */
	public static String numToLetter(String input) {
		StringBuffer result = new StringBuffer();
		if (!StringUtils.isEmpty(input)) {
			for (byte b : input.getBytes()) {
				result.append((char) (b + 48));
			}
		}
		return result.toString().toUpperCase();
	}
	
	/**
	 * 将字母转换成数字  
	 * @param input
	 * @return
	 */
    public static String letterToNum(String input) {
    	StringBuffer result = new StringBuffer();
        for (byte b : input.getBytes()) {  
            result.append(b - 96);
        }  
        return result.toString();
    }  

	private static OrganizationService organizationService;
	private static RoleService roleService;
	private static ResourceService resourceService;
	
	public static OrganizationService getOrganizationService() {
		if (organizationService == null) {
			organizationService = SpringUtils.getBean(OrganizationService.class);
		}
		return organizationService;
	}
	
	public static RoleService getRoleService() {
		if (roleService == null) {
			roleService = SpringUtils.getBean(RoleService.class);
		}
		return roleService;
	}
	
	public static ResourceService getResourceService() {
		if (resourceService == null) {
			resourceService = SpringUtils.getBean(ResourceService.class);
		}
		return resourceService;
	}
	
}
